package com.bjsxt.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.commons.DataGridModel;
import com.bjsxt.commons.MedicineModel;
import com.bjsxt.mapper.DrugMapper;
import com.bjsxt.mapper.ProviderMapper;
import com.bjsxt.pojo.Drug;
import com.bjsxt.pojo.DrugExample;
import com.bjsxt.pojo.UsersExample.Criteria;
import com.bjsxt.service.MedicineMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 药品信息管理实现类
 * @author clw
 *
 */
@Service
public class MedicineMessageServiceImpl implements MedicineMessageService {
	@Autowired
	private DrugMapper drugMapper;
	
	@Autowired
	private ProviderMapper providerMapper;
	
	@Override
	public DataGridModel showMedicine(int page, int rows) {
		//调用分页插件进行分页
		PageHelper.startPage(page, rows);
		DrugExample de = new DrugExample();
		List<Drug> list = this.drugMapper.selectByExample(de);
		
		for (int i = 0; i < list.size(); i++) {
			Drug drug = list.get(i);
			drug.setProvidername(providerMapper.selectByPrimaryKey(drug.getProviderid()).getProvidername());
		}
		
		PageInfo<Drug> p = new PageInfo<>(list);
		long totle = p.getTotal();
		DataGridModel dgm = new DataGridModel();
		dgm.setTotal(totle);
		dgm.setRows(list);
		return dgm;
	}

	@Override
	public void updateMedicine(Drug drug) throws Exception {
		DrugExample de = new DrugExample();
		com.bjsxt.pojo.DrugExample.Criteria criteria = de.createCriteria();
		criteria.andDrugidEqualTo(drug.getDrugid());
		drugMapper.updateByExampleSelective(drug, de);
	}

	@Override
	public void dropMedicineMessage(Integer drugid) {
			this.drugMapper.deleteByPrimaryKey(drugid);
	}

}
